<template>
<el-dialog :title="dialog.title" :visible.sync="dialog.visible" width="30%">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-position="top">
        <el-form-item label="状态名称" prop="title">
            <el-input v-model="ruleForm.title" placeholder="请填写回访状态名称"></el-input>
        </el-form-item>

        <el-form-item label="状态类型" prop="is_batch">
            <el-select v-model="ruleForm.is_batch" placeholder="状态类型">
                <el-option label="初始状态" :value="0"></el-option>
                <el-option label="跟进状态" :value="1"></el-option>
                <el-option label="产品意向" :value="2"></el-option>
            </el-select>
        </el-form-item>

        <el-form-item label="快捷回访">
            <el-input placeholder="添加" v-model="fastVal" clearable>
                <template slot="suffix">
                    <el-button type="primary" @click="appendFast">
                        <i class="el-icon-plus"></i>
                    </el-button>
                </template>
            </el-input>
            <template v-for="(item, key) in ruleForm.fast">
                <el-tag :key="key" type="primary" closable @close="removeFastItem(key)">{{ item }}</el-tag>
            </template>
        </el-form-item>

        <el-form-item label="下次跟进">
            <el-radio-group v-model="ruleForm.need_next_visit">
              <el-radio :label="1">开启</el-radio>
              <el-radio :label="0">关闭</el-radio>
            </el-radio-group>
        </el-form-item>

        <el-form-item label="排序" prop="sort">
            <el-input v-model="ruleForm.sort"></el-input>
        </el-form-item>

        <el-form-item label="是否开启">
            <el-radio-group v-model="ruleForm.status">
              <el-radio :label="1">开启</el-radio>
              <el-radio :label="0">关闭</el-radio>
            </el-radio-group>
        </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
        <el-button @click="dialog.visible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
    </div>
</el-dialog>
</template>

<script>
import {
    createStatusApi,
    saveStatusApi
} from "@/api/system/dictionary/status";
import {
    getFormToken
} from "@/api/system/system";

const ruleForm = {
    id: "",
    title: "",
    is_batch: "",
    sort: 0,
    status: 1,
    fast: [],
    need_next_visit: "0",
    form_token: "",
};

export default {
    data() {
        return {
            dialog: {
                title: "",
                visible: false,
                type: "",
            },
            fastVal: "",
            ruleForm: Object.assign({}, ruleForm),
            rules: {
                title: [{
                        required: true,
                        message: "回访状态名称必填",
                        trigger: "blur",
                    },
                    {
                        min: 2,
                        max: 30,
                        message: "长度在 2 到 30 个字符",
                        trigger: "blur",
                    },
                ],
                is_batch: [{
                        required: true,
                        message: "请选择状态类型",
                        trigger: "taggle",
                    },

                ],
            },
        };
    },
    methods: {
        showAddView() {
            let dialog = {
                title: "添加回访状态",
                visible: true,
                type: "create",
            };
            this.dialog = dialog;
            // 初始化表单
            this.ruleForm = Object.assign({}, ruleForm);
            // 获取表单的token
            getFormToken({
                api: "/api/system.source/create",
            }).then(res => {
                this.ruleForm.form_token = res.data.token;
            });
        },
        showSaveView(row) {

            let dialog = {
                title: "编辑回访状态",
                visible: true,
                type: "save",
            };
            this.dialog = dialog;
            // 初始化表单
            this.ruleForm = Object.assign({}, row);
            // 获取表单的token
            getFormToken({
                api: "/api/system.source/save",
            }).then(res => {
                this.ruleForm.form_token = res.data.token;
            });
        },
        appendFast(){
            let val = this.fastVal.trim();
            if (!val) {
                return false;
            }
            
            if (!this.ruleForm.fast.includes(val)) {
                this.ruleForm.fast.push(val);
                this.fastVal = "";
            } else {
               this.$message.error("请勿重复添加");
            }
        },
        removeFastItem(key) {
            this.ruleForm.fast.splice(key, 1);
        },
        submit() {
            this.$refs["ruleForm"].validate((valid) => {
                // 验证失败阻止提交
                if (!valid) return false;
                // 提交表单
                if (this.dialog.type == "create") {
                    createStatusApi(this.ruleForm)
                        .then((res) => {
                            if (res.code == "200") {
                                this.dialog.visible = false;
                                this.$emit("getTableData", {});
                                this.$message.success(res.msg);
                            } else {
                                this.$message.error(res.msg);
                            }
                        })
                        .catch((res) => {
                            this.$message.error(res.msg);
                        });
                } else {

                    saveStatusApi(this.ruleForm)
                        .then((res) => {
                            if (res.code == "200") {
                                this.dialog.visible = false;
                                this.$emit("getTableData", {});
                                this.$message.success(res.msg);
                            } else {
                                this.$message.error(res.msg);
                            }
                        })
                        .catch((res) => {
                            this.$message.error(res.msg);
                        });
                }
            });
        },
    },
};
</script>
